import { Loading } from 'element-ui'
import { ElLoadingComponent } from 'element-ui/types/loading'

class GlobalLoading {
  static loadingCount = 0
  static loadingInstance: ElLoadingComponent | null = null

  static showLoading(options = {}) {
    const loadingOptions = {
      text: '加载中...',
      lock: true,
      fullscreen: true,
      spinner: 'el-icon-loading',
      customClass: 'global-loading-element',
      ...options
    }
    if (GlobalLoading.loadingCount <= 0) {
      GlobalLoading.loadingInstance = Loading.service(loadingOptions)
    }
    GlobalLoading.loadingCount++
  }

  static hiddenLoading() {
    GlobalLoading.loadingCount--
    if (GlobalLoading.loadingCount <= 0 && GlobalLoading.loadingInstance) {
      GlobalLoading.loadingInstance.close()
      GlobalLoading.loadingInstance = null
    }
  }
}

export default GlobalLoading
